package com.glamour.hlcity.module.spider.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.glamour.hlcity.module.dom.analysis.DomAnalysis;
import com.glamour.hlcity.module.dom.analysis.impl.HtmlDomAnalysis;
import com.glamour.hlcity.module.url.analysis.UrlAnalysis;
import com.glamour.hlcity.module.url.analysis.impl.UrlAnalysisImpl;
import com.glamour.hlcity.modules.spider.parser.DomParser;
import com.glamour.hlcity.modules.spider.parser.impl.HtmlDomParser;

/**
 * 创建实例
 * @author zhaoxing.liuzx
 *
 */
public class BeanFactory {
	//bean 前缀
	private final static String PREFIX_BEAN_NAME="GLAMOUR_HLCITY_BEANS";
	//保存bean实例
	private static Map<String,Object> beanMap=new HashMap<String,Object>();

	/**
	 * 通过类型获得网页分析对象
	 * @param type
	 * @return
	 */
	public static DomAnalysis createDomAnalysis(String type){
		String beanName=PREFIX_BEAN_NAME.concat(type);
		if(beanMap.containsKey(beanName)){
			return (DomAnalysis) beanMap.get(beanName);
		}
		if(DomAnalysis.BEAN_HTML_DOMANALYSIS.equals(type)){
			DomAnalysis temp=new HtmlDomAnalysis();
			beanMap.put(beanName, temp);
			return temp;
		}
		return null;
	}

	/**
	 * 通过类型获得网页分析对象
	 * @param type
	 * @return
	 */
	public static DomParser createDomParser(String type){
		String beanName=PREFIX_BEAN_NAME.concat(type);
		if(beanMap.containsKey(beanName)){
			return (DomParser) beanMap.get(beanName);
		}
		if(DomParser.BEAN_NAME_HTML_DOM_PARSER.equals(type)){
			DomParser temp=new HtmlDomParser();
			beanMap.put(beanName, temp);
			return temp;
		}
		return null;
	}
	
	/**
	 * 通过类型获得URL分析对象
	 * @param type
	 * @return
	 */
	public static UrlAnalysis createUrlAnalysis(String type){
		String beanName=PREFIX_BEAN_NAME.concat(type);
		if(beanMap.containsKey(beanName)){
			return (UrlAnalysis) beanMap.get(beanName);
		}
		if(UrlAnalysis.BEAN_NAME_URL_ANALYSIS.equals(type)){
			UrlAnalysis temp=new UrlAnalysisImpl();
			beanMap.put(beanName, temp);
			return temp;
		}
		return null;
	}


}
